# -*- coding: utf-8 -*-
import sys
sys.path.append("../../quidditas")

from random import randint

import pygame

import quidditas
from pygame.time import Clock
from pygame.image import load

import type_definitions, processors, components

to_update = []
to_render= []

def main():
    """ Starting function for the application. Initialize pygame and quidditas and start
        the game.
    """
    pygame.init()
    pygame.display.set_caption("Tower defense example using Quidditas")
    
    screen = pygame.display.set_mode((640, 480))
    clock = Clock()
    running = 1
    
    images = {}
    images["soldier"] = load("res/soldier.png")

    q = init_quidditas(screen, images)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = 0
        q.update(clock.get_time())
        update(clock.get_time())
        render(clock.get_time())

        pygame.display.flip()
        clock.tick(60)

def update(time):
    """ Call all game logic processors. """
    [processor.update(time) for processor in to_update]

def render(time):
    """ Call all rendering processors. """
    [processor.update(time) for processor in to_render]

def init_quidditas(screen, images):
    """ Initialize the quidditas system and the processors. """
    q = quidditas.Quidditas()
    q.add_type_definitions(type_definitions.definitions)
    to_render.append(q.add_processor(processors.RenderProcessor(screen, images)))

    to_update.append(q.add_processor(processors.RoutePathfindingProcessor()))
    to_update.append(q.add_processor(processors.MovementProcessor((640, 480))))
    to_update.append(q.add_processor(processors.GameController(q)))
    to_update.append(q.add_processor(processors.WaveProcessor()))
    to_update.append(q.add_processor(processors.SpawnProcessor()))
    to_update.append(q.add_processor(processors.PathfindingProcessor()))
    
    return q

if __name__ == "__main__":
    main()
